package com.eveinfo.display.planner;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import android.content.Context;

import com.eveinfo.EveInfo;
import com.eveinfo.api.utils.skilllist.ApiSkill;
import com.eveinfo.api.utils.skilllist.ApiSkills;

public class PlannerManager implements Serializable {

	private static final long serialVersionUID = 3120207434994814173L;

	public static String CACHE_DIR = "/data/data/com.eveinfo/app_planner";
	private static final String fileName = "planners";
	
	private final ArrayList<Planner> planners;
	private final Context context;
	private final String characterName;

	public PlannerManager(Context context, String characterName) {
		this.context = context;
		planners = new ArrayList<Planner>(3);
		this.characterName = characterName;
		deSerializeCache();
	}

	public void createPlanner(String plannerName, int characterID) {
		Planner planner = new Planner(plannerName, characterID);
		planners.add(planner);
		serializeCache();
	}

	public Planner removePlanner(int position) {
		Planner remove = planners.remove(position);
		serializeCache();
		return remove;
	}

	private String getFileName() {
		return fileName + "_" + characterName;
	}

	private void serializeCache() {
		try {
			File file = new File(CACHE_DIR, getFileName());
			file.createNewFile();
			FileOutputStream fos = new FileOutputStream(file);
			ObjectOutputStream out = new ObjectOutputStream(fos);
			out.writeObject(planners);
			out.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public void deSerializeCache() {
		try {
			File file = new File(CACHE_DIR, getFileName());
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream in = new ObjectInputStream(fis);
			try {
				ArrayList<Planner> oldCache = (ArrayList<Planner>) in.readObject();
				planners.addAll(oldCache);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			in.close();
			fis.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public Planner getPlanner(String plannerName) {
		for (Planner planner : planners) {
			if (planner.getName().equals(plannerName)) {
				return planner;
			}
		}
		return null;
	}

	public ArrayList<String> getPlannersName() {
		ArrayList<String> names = new ArrayList<String>(6);
		for (Planner planner : planners) {
			names.add(planner.getName());
		}
		return names;
	}

	public void addSkillToPlanner(String plannerName, ApiSkill skill, int level) {
		Planner planner = getPlanner(plannerName);
		planner.addSkill(skill, level);
		serializeCache();
	}

	public void addSkillToPlanner(String plannerName, int skillID, int level) {
		ApiSkills allSkill = EveInfo.getData().getSkills();
		ApiSkill skill = allSkill.getSkill(skillID);
		addSkillToPlanner(plannerName, skill, level);
	}

	public void removeSkillFromPanner(String plannerName, int id) {
		Planner planner = getPlanner(plannerName);
		planner.removeSkill(id);
		serializeCache();
	}

	public int getSize() {
		return planners.size();
	}

	public Planner getPlanner(int position) {
		return planners.get(position);
	}

}
